module.exports = {

  auth: options=>{
    const util = require('../util/util')
    const jwt = require('jsonwebtoken')
    const AdminUser = require('../models/AdminUser')
    return async (req,res,next)=>{
      let token = String(req.headers.authorization || '').split(' ').pop()
      if(!token){
        res.send(util.fail({message:'请先登录',code:401}))
      }
      
      jwt.verify(token,req.app.get('secret'),function(err,decoded){
        if(err || !decoded){
          res.send(util.fail({message:'请先登录',code:401}))
        }
        let user = AdminUser.findById(decoded._id)
        if(!user){
          res.send(util.fail({message:'请先登录',code:401}))
        }
      })
      
      await next()
    }
  }
}